import datetime

from utils.acl import acl
from utils.jwtc import token1
from flask import Flask, request, jsonify
import logging
from logging import FileHandler

# 注册蓝图
from views.comany import bp_comany
from views.user import bp_user

# 跨域
from flask_cors import CORS

# 定义全局app
def create_app():
    app = Flask(__name__)
    with app.app_context():
        pass
    return app
app = create_app()

# 跨域
CORS(app)

# 注册蓝图
app.register_blueprint(bp_comany)
app.register_blueprint(bp_user)



@app.before_request
def before_request():
    white = ["/login"]
    try:
        token = request.headers.get("Token")
        print(token)
    except:
        token = None
    if token:
        user = token1.decode_jwt(token,"123")
        print(user)
        if user:
            reslist = acl.get_page_reslist(user["id"])
            print(reslist)
            for i in reslist:
                white.append(i)
            if request.path not in white:
               return jsonify({"code":403,"msg":"fail"})
            else:
                return jsonify({"code": 40313, "msg": "no authorzation"})
        else:
            return jsonify({"code":40312,"msg":"你的token有问题"})
    else:
        if request.path not in white:
            return jsonify({"code":40311,"msg":"没有token"})


if __name__ == '__main__':
    filename = datetime.datetime.strftime(datetime.datetime.now(),'%Y%m')
    handler = logging.FileHandler("logs/"+filename+'.txt')
    app.logger.addHandler(handler)
    app.run(debug=True)









